package cn.stylefeng.guns.modular.system.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.system.entity.InventoryOrder;
import cn.stylefeng.guns.modular.system.entity.InventorySales;
import cn.stylefeng.guns.modular.system.model.params.InventoryOrderParam;
import cn.stylefeng.guns.modular.system.model.params.InventorySalesParam;
import cn.stylefeng.guns.modular.system.model.result.InventoryOrderResult;
import cn.stylefeng.guns.modular.system.service.InventoryOrderService;
import cn.stylefeng.guns.modular.system.service.InventorySalesService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;


/**
 * 出库单控制器
 *
 * @author 小飞
 * @Date 2019-07-08 15:07:49
 */
@Controller
@RequestMapping("/inventorySales")
public class InventorySalesController extends BaseController {

    private String PREFIX = "/inventorySales";

    @Autowired
    private InventoryOrderService inventoryOrderService;

    @Autowired
    private InventorySalesService inventorySalesService;

    /**
     * 跳转到主页面
     *
     * @author 小飞
     * @Date 2019-07-08
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/inventorySales.html";
    }

    /**
     * 新增页面
     *
     * @author 小飞
     * @Date 2019-07-08
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/inventorySales_add.html";
    }

    /**
     * 编辑页面
     *
     * @author 小飞
     * @Date 2019-07-08
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/inventorySales_edit.html";
    }

    /**
     * 新增接口
     *
     * @author 小飞
     * @Date 2019-07-08
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(InventorySalesParam inventorySalesParam) {
        this.inventorySalesService.add(inventorySalesParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author 小飞
     * @Date 2019-07-08
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(InventorySalesParam inventorySalesParam) {
        this.inventorySalesService.update(inventorySalesParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author 小飞
     * @Date 2019-07-08
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(InventorySalesParam inventorySalesParam) {
        this.inventorySalesService.delete(inventorySalesParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author 小飞
     * @Date 2019-07-08
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(InventorySalesParam inventorySalesParam) {
        InventorySales detail = this.inventorySalesService.getById(inventorySalesParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author 小飞
     * @Date 2019-07-08
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(InventorySalesParam inventorySalesParam) {
        return this.inventorySalesService.findPageBySpec(inventorySalesParam);
    }


    /**
     * 查询库存表其中一条数据，把它添加到出库单中
     * 根据商品编号进行查询
     */
    @RequestMapping("/chuku")
    public String chuku(String serialNum) {
        //查询库存表其中一条数据
        InventoryOrderResult inventoryOrderResult = inventoryOrderService.getInventoryOrderId("adsasdas");

        //把库存表的数据添加到出库单中
        InventorySalesParam inventorySales = new InventorySalesParam();
        //存入库存id
        inventorySales.setInventoryOrderId(inventoryOrderResult.getId());
        //存入数量
        long num = inventoryOrderResult.getAmount();
        BigDecimal totalFee = new BigDecimal(num);
        BigDecimal d100 = new BigDecimal(1);
        BigDecimal fee = totalFee.divide(d100,2,2);//小数点2位
        inventorySales.setAmount(fee);
        //存入产品单价
        //inventorySales.setPrice(inventoryOrderResult);
        inventorySales.setSerialNum(inventoryOrderResult.getSerialNum());
        inventorySalesService.add(inventorySales);

        return "/inventoryOrder/ok.html";
    }











}


